package com.jd.util;

import java.text.NumberFormat;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;


public final class StringUtils {
	private StringUtils() {}
	
	public static String convertNull(String source) {
		if(source == null) return ""; return source;
	}
	public static Integer integerNull(Integer source) {
		if(source == null) return -1; return source;
	}
	
	//double转String
    public static String douToStr(double dou) {
        Double dou_obj = new Double(dou);
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        String dou_str = nf.format(dou_obj);
        if(dou_str == null) return ""; return dou_str;
    }
   
    //String转double
    public static Double strToDou(String str) {
    	Double dou = Double.valueOf(str);
    	return dou;
    }


    /**
     * @param strlength 是指定输出多少个字符串
     * @return
     */
    public static String zfc(int strlength) {
        String string="0123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
        //随机生成的字符串的所有字符都来自string字符串
        Random random=new Random();
        Set<String> set=new HashSet<String>();
        //存随机生成的字符串，保证不重复
        Set<Integer> set2=new HashSet<Integer>();
        //存随机生成数，保证不重复
        StringBuffer sfBuffer=new StringBuffer();

        sfBuffer.setLength(0);//清空sfBuffer
        set2.clear();//清空set2
        while(set2.size()<strlength)
        {
            //生成10个随机不重复的数，存入到set2中;
            int num=random.nextInt(62);
            set2.add(num);
        }
        for(Integer a1:set2) {
            sfBuffer.append(string.charAt(a1));//遍历set2，生成随机字符串
        }
        return sfBuffer.toString();//添加随机字符串到set中
    }
}
